package com.jt.test;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Money {
    private String name;
    private double number;
    private Lock lock = new ReentrantLock();

    public Money() {
    }

    public Money(String name, Double number) {
        this.name = name;
        this.number = number;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getNumber() {
        return number;
    }

    public void setNumber(Double number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "Money{" +
                "name='" + name + '\'' +
                ", number=" + number +
                '}';
    }


    public void quqian(double yueMoney) {
        String name = Thread.currentThread().getName();
        /*枷锁*/
        lock.lock();
        try {
            System.out.println("this.number========" + this.number);
            if (this.number >= yueMoney) {
                System.out.println(name + "来取钱" + yueMoney + "成功");
                this.number -= yueMoney;
                System.out.println(name + "来取钱，剩余余额=" + this.number);
            } else {
                System.out.println(name + "来取钱。余额不足");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }
}
